<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>文章列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="key" label="ID" width="100"></el-table-column>
          <el-table-column prop="title" label="标题"></el-table-column>
          
          <el-table-column prop="inputtime" label="发布时间" width="260" sortable>
            <template slot-scope="scope">{{scope.row.inputtime | formateDate}}</template>
          </el-table-column>
          <el-table-column prop="author" label="作者" width="200"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <!-- 点击编辑按钮，需要把当前这条信息的id传给跳转的那个页面 -->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                circle
                @click="$router.push({path:'/index/article/edit', query: { id: scope.row.id}})"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="del(scope.row.id)"
                circle
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="page">
          <!-- layout, 显示的内容：prev 上一页； next 下一页； pager 页码列表； sizes 每页多少条， jumper 跳转第n页-->
          <el-pagination
            background
            layout="total, prev, pager, next, sizes, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 30, 50]"
          ></el-pagination>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/api/article";
import "@/utils/filters.js";
export default {
  data() {
    return {
      tableData: [],
      curr: 1, // 当前第几页
      limit: 10, // 每页多少条
      total: 100, // 一共多少条
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    // ajax请求获取口碑列表
    getlist() {
      request.getArticleList({ curr: this.curr, limit: this.limit }).then((r) => {
        // console.log(r.data);
        this.total = r.data.total;
        // 遍历r.data.list数组， 给每一项添加一个 key, 第一页key(1,2,3...10); 第2页key(11,12,13,...20)
        r.data.listData.forEach((item,index) => {
            item.key = index + 1 + (this.curr - 1) * this.limit
        });
        this.tableData = r.data.listData;
      });
    },

    // 事件：改变当前第几页
    handleCurrentChange(val) {
      this.curr = val;
      this.getlist();
    },

    // 事件：改变每页多少条
    handleSizeChange(val) {
      this.limit = val;
      this.getlist();
    },

    del(myid) {

      // 确认框
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除的ajax操作
          request.delOneArticle({ id: myid }).then((r) => {
            if (r.data.code === 0) {
              this.$message({
                type: "success",
                message: "删除成功！",
              });
              this.getlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.page {
  margin-top: 15px;
}

</style>